package com.zhp;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import java.io.File;
import java.util.Collections;

/**
 * @author zhanghp
 * @since 2024/5/14 10:47
 */
public class Generator {

    private static final String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";

    private static final String user = "root";

    private static final String password = "zhp.1221";

    private static final String module_name = "spring-boot-lab";

    private static final String module = System.getProperty("user.dir") + File.separator+ module_name;

    private static final String directory = module + "/src/main/java";

    private static final String target = "target";

    public static void main(String[] args) {

        String table ="user_info";
        FastAutoGenerator.create(url, user, password).globalConfig(a -> {
            a.author("zhp").fileOverride().outputDir(directory);
        }).packageConfig(a -> {
            a
                    .parent("com.zhp")

                    .controller("ctrl")
                    .service("service")
                    .serviceImpl("service.impl")
                    .entity("dao.entity")
                    .mapper("dao.mapper")
                    .xml("mapper.xml")
                    .pathInfo(Collections.singletonMap(OutputFile.mapperXml, module + "/src/main/resources/mapper/"));
        }).strategyConfig(a -> {
            a
                    .addInclude(table.split(","))
                    .controllerBuilder().enableRestStyle().formatFileName("%sCtrl")
                    .serviceBuilder().formatServiceFileName("%sService").formatServiceImplFileName("%sServiceImpl")
                    .entityBuilder().enableLombok().naming(NamingStrategy.underline_to_camel).columnNaming(NamingStrategy.underline_to_camel)
                    .mapperBuilder().superClass(BaseMapper.class).enableMapperAnnotation().enableBaseResultMap().enableBaseColumnList();
                }
        ).templateEngine(new VelocityTemplateEngine()).execute();
    }
}
